package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//地址全称为:http://localhost:8083/demo20211015/login.do
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决响应中文乱码
        resp.setContentType("text/html;charset=utf-8");
        //获取前端传来的值
        //req.getParameter通过请求的name获取到请求的值
        //跟前端input name一致
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("用户名为：" + username);
        System.out.println("密码为：" + password);
        if ("admin".equals(username) && "123".equals(password)) {
            //告诉用户登录成功
            //获取响应的字符输出流
//            resp.getWriter().write("<h1>" + username + "登录成功</h1>");
            //请求重定向
            //http://localhost:8083/ 这里的斜杠是唯一一个后端特殊的斜杠
            //和前端的/意思相同
            //因为这个字符串 最终是给浏览器进行解析跳转的
            resp.sendRedirect("./index.html");
        } else {
            resp.sendRedirect("./login.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
